<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/2/12
 * Time: 11:30 AM
 * To change this template use File | Settings | File Templates.
 */
// No direct access.
defined('_JEXEC') or die;

// Include dependancy of the main controllerform class
jimport('joomla.application.component.controllerform');

class VastControllerBaocao extends JControllerForm
{

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

    public function submit()
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');
        
        $type = JRequest::getInt('baocaoLoai', 0);
                                
        if ($type == 1)
        {
            $this->setRedirect(JRoute::_('index.php?option=com_vast&view=baocao&type=1'));
        }
        return true;
    }

    public function export()
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');
        $tuoiNam = JRequest::getInt('tuoiNam', 60);
        $tuoiNu = JRequest::getInt('tuoiNu', 55);
        
        $this->setRedirect(JRoute::_('index.php?option=com_vast&view=baocao&layout=view&nam=' . $tuoiNam . '&nu=' . $tuoiNu));
        
        return true;
    }

    public function csvReport()
    {
        $tuoiNam = JRequest::getInt('nam', 60);
        $tuoiNu = JRequest::getInt('nu', 55);

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.hoten AS `Họ tên`, a.ngaysinh AS `Ngày sinh`, a.nguyenquan AS `Nguyên quán`, a.dienthoai AS `Số điện thoại`, a.thudientu AS `Thư điện tử`');
        $query->from('#__hosocanbo AS a');
        $query->where('(DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNu . ' AND `gioitinh` = 0) OR (DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNam . ' AND `gioitinh` = 1)');
        
        $db->setQuery($query);
        
        $result = $db->loadObjectList();        
                
        if (count($result) > 0)
        {
            mb_internal_encoding("UTF-8");
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment;filename=vastreport.csv');
            
            if ($fp = fopen('php://output', 'w')) {
                if ($result[0]) {
                    fputcsv($fp, array_keys(JArrayHelper::fromObject($result[0])));                    
                }
                foreach ($result as $row) {
                    fputcsv($fp, JArrayHelper::fromObject($row));                
                }
                fclose($fp);
            }        
            
            JFactory::getApplication()->close();
        }
        else
        {
            return false;
        }
    }
    
    public function excelReport()
    {

        $tuoiNam = JRequest::getInt('nam', 60);
        $tuoiNu = JRequest::getInt('nu', 55);

        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.hoten AS `Họ tên`, a.ngaysinh AS `Ngày sinh`, a.nguyenquan AS `Nguyên quán`, a.dienthoai AS `Số điện thoại`, a.thudientu AS `Thư điện tử`');
        $query->from('#__hosocanbo AS a');
        $query->where('(DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNu . ' AND `gioitinh` = 0) OR (DATEDIFF(CURDATE(), `ngaysinh`)/365 >= ' . $tuoiNam . ' AND `gioitinh` = 1)');
        
        $db->setQuery($query);
        
        $result = $db->loadObjectList();        
                
        if (count($result) > 0) 
        {
            $table = $display = "";	
            $fn = "vastreport";
    			
            $table .= '<table border="0" cellpadding="0" cellspacing="0" id="ctbl"><tr><td>';
            $table .= '<tr id="atd">';
            $table .= '<td rowspan="2" style="background-color:#000099;color:#FFFFFF;">Time</td>';
            $table .= '<td colspan="4" style="background-color:#FFFF33">TN</td>';
            $table .= '<td colspan="4" style="background-color:#FFFF33">CN</td>';
            $table .= '<td rowspan="2" style="background-color:#000099;color:#FFFFFF;padding:0px 5px 0px 5px;">Band<br>Level</td>';
            $table .= '</tr>';
            $table .= '<tr id="atd">';
            $table .= '<td style="background-color:#FFFCCC">OFFERED</td>';
            $table .= '<td style="background-color:#FFFCCC">BAND</td>';
            $table .= '<td style="background-color:#FFFCCC">RUN TIME</td>';
            $table .= '<td style="background-color:#FFFCCC">Abandoned</td>';
            $table .= '<td style="background-color:#FFCC99">OFFERED</td>';
            $table .= '<td style="background-color:#FFCC99">BAND</td>';
            $table .= '<td style="background-color:#FFCC99">RUN TIME</td>';
            $table .= '<td style="background-color:#FFCC99">Abandoned</td>';
            $table .= '</tr>';
            $table .= '<tr>';
            $table .= $display;
            $table .= '</td></tr></table>';
    		
    
    		header("Content-type: application/x-msdownload"); 
    		header("Content-Disposition: attachment; filename=$fn.xls");
    		header("Pragma: no-cache");
    		header("Expires: 0");
    		echo $table;                    
        }
        else
        {
            return false;
        }
    }
}